package icu.groot.alan.servlet.user;

import com.fasterxml.jackson.databind.ObjectMapper;
import icu.groot.alan.entity.TokenEntity;
import icu.groot.alan.service.impl.UserServiceImpl;
import icu.groot.alan.util.Alan;
import icu.groot.alan.util.R;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

@WebServlet("/user/update.do")
public class UserUpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String code = req.getParameter("code");
        ObjectMapper objectMapper = new ObjectMapper();
        if (code == null || code.equals("")){
            resp.getWriter().print(objectMapper.writeValueAsString(R.error("缺少参数,请联系管理员")));
            return;
        }
        TokenEntity tokenEntity = (TokenEntity) req.getAttribute("tokenEntity");
        String codeInToken = tokenEntity.getCode();
        if (!codeInToken.equals(code) && !tokenEntity.getDepartment().equals("管理员") || code.equals("10000001")){
            resp.getWriter().print(objectMapper.writeValueAsString(R.error("权限不够")));
            return;
        }
        String name =  req.getParameter("name");
        String gender =  req.getParameter("gender");
        String birthDate =  req.getParameter("birthDate");
        String jobTitle =  req.getParameter("jobTitle");
        String education =  req.getParameter("education");
        String degree =  req.getParameter("degree");
        String qq =  req.getParameter("qq");
        String email =  req.getParameter("email");
        String introduction =  req.getParameter("introduction");
        String departmentId =  req.getParameter("departmentId");
        String password =  req.getParameter("password");
        //校验一些不能为空的参数
        if (StringUtils.isAnyBlank(name, birthDate, gender, departmentId, password)){
            resp.getWriter().print(objectMapper.writeValueAsString(R.error("参数缺失")));
            return;
        }
        if (departmentId.equals("1") && !tokenEntity.getDepartment().equals("管理员")){
            resp.getWriter().print(objectMapper.writeValueAsString(R.error("权限不够")));
            return;
        }
        if (StringUtils.isAnyBlank(jobTitle)) jobTitle = "未知...";
        if (StringUtils.isAnyBlank(education)) education = "未知...";
        if (StringUtils.isAnyBlank(degree)) degree = "未知...";
        if (StringUtils.isAnyBlank(introduction)) introduction = "暂无...";
        //校验姓名,不能有数字,防止和工号重名
        if (name.matches(".*[0-9].*")){
            resp.getWriter().print(objectMapper.writeValueAsString(R.error("姓名中不能含有数字")));
            return;
        }
        //限制性别参数
        if (!gender.equals("男") && !gender.equals("女")) {
            resp.getWriter().print(objectMapper.writeValueAsString(R.error("性别参数错误")));
            return;
        }
        try {
            //限制出生日期参数，不能超过当前时间
            if (Alan.strToLocalDate(birthDate, Alan.FORMAT_LOCAL_DATE).isAfter(LocalDate.now())) {
                resp.getWriter().print(objectMapper.writeValueAsString(R.error("出生日期不合常理")));
                return;
            }
        } catch (Exception e) {
            resp.getWriter().print(objectMapper.writeValueAsString(R.error("出生日期参数错误")));
            return;
        }
        //校验qq参数
        if (StringUtils.isAnyBlank(qq)){
            qq = "未知...";
        }else if (!qq.matches("[1-9][0-9]{4,9}")){
            resp.getWriter().print(objectMapper.writeValueAsString(R.error("qq参数错误")));
            return;
        }
        //校验邮箱
        if (StringUtils.isAnyBlank(email)){
            email = "未知...";
        }else if (!email.matches("[a-zA-Z0-9]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]+")){
            resp.getWriter().print(objectMapper.writeValueAsString(R.error("邮箱参数错误")));
            return;
        }

        resp.getWriter().print(objectMapper.writeValueAsString(new UserServiceImpl().updateUserByCode(code, name, gender, birthDate, jobTitle, education, degree, qq, email, introduction, departmentId, password)));
    }
}
